package com.itheima.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.admin.mapper.UserMapper;
import com.itheima.admin.pojo.Score;
import com.itheima.admin.pojo.User;
import com.itheima.admin.service.UserService;
import com.itheima.admin.util.Result;
import lombok.experimental.Helper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result getAllUser(Integer currentPage,Integer size) {
        PageHelper.startPage(currentPage,size);
        List<User> users = userMapper.selectAllUser();
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        return Result.success(200,"查询成功",userPageInfo);
    }

    @Override
    public Result insertUser(User user) {
        userMapper.insertUser(user);
        if(user.getStatus().equals("学生")){
           for(int i=0;i<5;i++){
               userMapper.insertScore();

           }
            User user1 = userMapper.selectSingleUserByStuID(user.getStuID());
            List<Integer> integers = userMapper.selectScoreId();
            userMapper.insertUserScoreKecheng(integers,user1.getId());
        }
        return Result.success(200,"插入成功");
    }

    @Override
    public Result deleteUser(Integer id) {
        Integer integer = userMapper.deleteUser(id);
        if(integer==0){
            return Result.error("删除失败");
        }
        return Result.success(200,"删除成功");
    }

    @Override
    public Result updateUser(User user,Integer id) {
        Integer integer = userMapper.updateUser(user,id);
        if(integer==0){
            return Result.error("更新失败");
        }
        return Result.success(200,"更新成功");
    }

    @Override
    public Result selectSingleUserById(Integer id) {
        User user = userMapper.selectSingleUserById(id);
        return Result.success(200,null,user);
    }

    @Override
    public Result selectSingleUserByStuID(String stuID) {
        User user = userMapper.selectSingleUserByStuID(stuID);
        List<User> users = new ArrayList<>();
        users.add(user);
        System.out.println(user);
        return Result.success(200,null,users);
    }

    @Override
    public Result selectAllScore(Integer currentPage,Integer size) {
        List<User> users = userMapper.selectAllScore();
        System.out.println(users);
        for(User user : users){
            int total = 0;
            for(Score score : user.getScores()){
                total+=score.getScore();
            }
            user.setTotal(total);
            total = 0;
        }
        ArrayList<User> newUsers = new ArrayList<>();
        int start = (currentPage-1) * size;
        if(start > users.size()-1){
            return Result.success(200,null,newUsers);
        }
        for(int i = start;i<start+size;i++){
            if(i<=users.size()-1){
                newUsers.add(users.get(i));
            }
        }
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        userPageInfo.setList(newUsers);
        userPageInfo.setTotal(users.size());

        return Result.success(200,"查询成功",userPageInfo);
    }


    @Override
    public Result selectSingleScoreByStuID(String stuID) {
        System.out.println(stuID);
        User user = userMapper.selectSingleScoreByStuID(stuID);
        int total = 0;
        for(Score score : user.getScores()){
          total += score.getScore();
        }
        user.setTotal(total);
        return Result.success(200,null,user);
    }
}
